import collections
def bfs(nums):
    n = len(nums)
    if n <=1:
        return len(nums)
    Q = collections.deque([(0,0)])
    visited = set([(0,0)])
    step = 1 #这里（0，0）也算
    while Q:
        s = len(Q)
        for i in range(s):
            cur = Q.popleft()
            x, y = cur[0], cur[1]
            if x == y and x == n-1: #判断是否到达终点
                return step
            for dx,dy in [(-1,0),(1,0),(0,-1),(0,1)]: #可行的相邻节点加入队列
                nx,ny = x+dx,y+dy
                if 0<=nx<n and 0<=ny<n: # 这里就能够解决在最上/最左/最右/最下的时候出现的限制
                    if nums[nx][ny] == 0 and (nx,ny) not in visited:
                        Q.append((nx,ny))
                        visited.add((nx,ny))
        step += 1
    return -1 #遍历完没有找到可行路径

firstline = list(map(int,input().split()))
n = len(firstline)
nums = []
nums.append(firstline)
for _ in range(n-1):
    info = list(map(int,input().split()))
    nums.append(info)
print(bfs(nums))


# 0 1 1 1 1
# 0 0 0 1 1
# 1 1 0 1 1
# 1 1 0 0 0
# 1 1 1 1 0